/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

sessinfo = {};

sessinfo.Application = function(){};
core.extendClass('sessinfo.Application', 'core.JSApplication');

sessinfo.SessionList = function(){};
core.extendClass('sessinfo.SessionList', 'core.JSFrame');

sessinfo.ChatHistory = function(){};
core.extendClass('sessinfo.ChatHistory', 'core.JSFrame');

sessinfo.NewMessageDialog = function(){};
core.extendClass('sessinfo.NewMessageDialog', 'core.JSDialog');

sessinfo.SessionList.prototype.sendMessageDlg = function(to){
    var instance = this;
    var textField = new core.form.TextField({
        colspan: 2
    });
    var lbl = new core.Label({
        text: 'Send message to: '+core.escapeHTML(to),
        colspan: 2,
        align: 'left'
    });
    var okBtn = new core.form.Button({
        text: 'Send',
        align: 'right',
        handler: function(){
            //Send here
            instance.sendMessage('acceptMessage', {
                message: textField.getValue(),
                to: to
            }, function(){
                core.desktop.closeDialog(dlg);
            }, function(code, text){
                core.desktop.closeDialog(dlg);
                instance.application.showError(text);
            }, dlg)
        }
    });
    var cancelBtn = new core.form.Button({
        text: 'Cancel',
        handler: function(){
            core.desktop.closeDialog(dlg);
        }
    });
    var dlg = new core.Dialog({
        title: 'Send message:',
        columns: '1, d',
        rows: 'd, d, d',
        items: [lbl, textField, okBtn, cancelBtn],
        maxWidth: 300
    });
    core.desktop.showDialog(dlg);
    textField.doFocus();
};

sessinfo.SessionList.prototype.refreshList = function(){
    var instance = this;
    this.sendMessage('refreshList', {}, function(data){
        instance.grid.clear();
        var arr = $.makeArray(data.sessions);
        for (var i = 0; i < arr.length; i++) {
            var a = core.newElement(null, 'a', 'send_message');
            $(a).text('Send message').click(function(e){
                instance.sendMessageDlg(e.target.login);
                return false;
            }).attr('href', '#');
            a.login = arr[i].login;
            //#ifdef DESKTOP
            instance.grid.appendRow([core.escapeHTML(arr[i].login), arr[i].latestActivity, a]);
            //#endif

            //#ifdef MOBILE
            instance.grid.appendRow([core.escapeHTML(arr[i].login), a]);
            //#endif
        };
        instance.grid.bodyChanged();
    }, null, this.grid);

};

sessinfo.SessionList.prototype.loadContents = function(){
    var instance = this;
    this.refreshButton = new core.form.Button({
        text: 'Reload',
        toolbarStyle: true,
        handler: function(){
            instance.refreshList();
        }
    });
    this.toolbar = new core.Tabs({
        toolbar: true,
        elements: [this.refreshButton]
    });
    this.frame.addToolbar(this.toolbar);
    //#ifdef DESKTOP
    this.grid = new core.grid.GridPanel({
        headerCaptions: ['Login:', 'Latest activity:', 'Send message:'],
        columns: '0.5, 0.5, 130'
    });
    //#endif
    //#ifdef MOBILE
    this.grid = new core.grid.GridPanel({
        headerCaptions: ['Login:', 'Send message:'],
        columns: '1, 100'
    });
    //#endif

    this.setContent([this.grid], '1', '1');
    this.refreshList();
};

sessinfo.NewMessageDialog.prototype.createDialog = function(aData){
    var instance = this;
    var txt = '<b>From:</b> '+core.escapeHTML(aData.from)+'<br/>'+
        '<b>When:</b> '+aData.receiveDate+'<br/>'+
        '<b>Message:</b> '+core.escapeHTML(aData.message);
    var dlg = core.createDialog(txt, 'New message from '+core.escapeHTML(aData.from)+':', 500, function(){
        instance.sendMessage('closeDialog', {}, null, null, instance.dialog);
    });
    return dlg;
};

sessinfo.ChatHistory.prototype.addMessage = function(data){
    var div = core.newDiv(this.chatLog.contentDiv, 'chat_entry');
    $(div).html('<b>'+data.receiveDate+'</b>: <i>'+core.escapeHTML(data.from)+'</i>: '+
        core.escapeHTML(data.message));
    this.chatLog.bodyChanged();
};

sessinfo.ChatHistory.prototype.loadContents = function(){
    var instance = this;
    this.chatLog = new core.HTMLPanel({
        html: ''
    });
    this.setContent([this.chatLog], '1', '1');
    this.sendMessage('getChatLog', {}, function(data){
        var arr = $.makeArray(data.log);
        for (var i = 0; i < arr.length; i++) {
            instance.addMessage(arr[i]);
        };
    });
};
